class BooksController < ApplicationController
 # before_filter :authenticate_user!, :except => [:show]
  # GET /books
  # GET /books.json
  def index
    if session[:user].role != "admin"
      redirect_to homepage_url
    else
      @books = Book.all

      respond_to do |format|
        format.html # index.html.erb
        format.json { render json: @books }
      end
    end
  end

  # GET /books/1
  # GET /books/1.json
  def show
    @book = Book.find(params[:id])
    @comments = Comment.find(:all,:conditions =>['book_id = ?',params[:id]],:order => 'time_cm')
    @users = @comments.map{|x| x.user}
    puts @users
    @profiles= @users.map{|x| UserProfile.find_by_user_id(x.id)}
    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @book }
    end
  end

  # GET /books/new
  # GET /books/new.json
  def new
    if session[:user].role != "admin"
      redirect_to homepage_url
    else
      @book = Book.new

      respond_to do |format|
        format.html # new.html.erb
        format.json { render json: @book }
      end
    end
  end

  # GET /books/1/edit
  def edit
    if session[:user].role != "admin"
      redirect_to homepage_url
    else
      @book = Book.find(params[:id])
    end
  end

  # POST /books
  # POST /books.json
  def create
    if session[:user].role != "admin"
      redirect_to homepage_url
    else
      @book = Book.create(params[:book])

      respond_to do |format|
        if @book.save
          if(session[:language] == "vi")
            format.html { redirect_to book_path(@book)+"?lang=vi", notice: 'Book was successfully created.' }
          else
            format.html { redirect_to @book, notice: "Book was successfully created." }
          end
          format.json { render json: @book, status: :created, location: @book }
        else
          format.html { render action: "new" }
          format.json { render json: @book.errors, status: :unprocessable_entity }
        end
      end
    end
  end

  # PUT /books/1
  # PUT /books/1.json
  def update
    if session[:user].role != "admin"
      redirect_to homepage_url
    else
      @book = Book.find(params[:id])

      respond_to do |format|
        if @book.update_attributes(params[:book])
          if(session[:language] == "vi")
            format.html { redirect_to book_path(@book)+"?lang=vi", notice: 'Book was successfully updated.' }
          else
            format.html { redirect_to @book, notice: 'Book was successfully updated.' }
          end
          format.json { head :no_content }
        else
          format.html { render action: "edit" }
          format.json { render json: @book.errors, status: :unprocessable_entity }
        end
      end
    end
  end

  # DELETE /books/1
  # DELETE /books/1.json
  def destroy
    if session[:user].role != "admin"
      if(session[:language] == "vi")
            redirect_to homepage_url+"?lang=vi"
          else
            redirect_to homepage_url
          end
      
    else
      listdelete=params[:bookdelete]
      if listdelete != nil
        listdelete.each do |e|
          @book= Book.find(e)
          @book.destroy
        end
      else

        @book = Book.find(params[:id])
      @book.destroy
      end
      respond_to do |format|
        format.html { 
          if(session[:language] == "vi")
            redirect_to books_url+"?lang=vi"
          else
            redirect_to books_url
          end
           }
        format.json { head :no_content }
      end
    end
  end

  def showrate
    @book = Book.find(params[:book_id])
    newrate = params[:star].to_f
    oldrate = @book.rate
    @book.rate = ((oldrate*@book.rate_number) + newrate)/(@book.rate_number+1)
    @book.rate = (@book.rate*10).round()/10.0
    @book.rate_number +=1
    @book.save
    puts newrate
    redirect_to @book
  end

  def language
    if params[:lang] == "vi"
      session[:language] = "vi"
      redirect_to params[:url]+"?lang="+session[:language]
    else
      session[:language] = "en"
      redirect_to params[:url]
    end
  end

end
